/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.report.gui;

import blueprint4j.db.*;
import blueprint4j.gui.*;
import blueprint4j.gui.db.*;
import blueprint4j.report.db.*;
import blueprint4j.utils.*;


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
import java.text.*;
import javax.swing.*;

public class PanelScheduleReport extends EntityEditor implements TabbedpaneGroup.TabSelectedInterface,EntityPanelNewEventListner{
    private ScheduleReport sc_report;
    private DBConnection connection = null;
    private BindFieldInterface bind_field = null;
    private String name[]=null,option[]=null;
    
    
    /**
     * An entity can either be scheduled for an entity or not
     */
    public PanelScheduleReport(DBConnection connection,BindFieldInterface bind_field,String name[],String option[])
    throws DataException, SQLException {
        super(connection,"Report Schedule");
        this.connection = connection;
        this.bind_field = bind_field;
        this.option = option;
        this.name = name;
        try {
            sc_report=new ScheduleReport(connection);
            setDetails(
                    sc_report,
                    new EntityPanelFieldBinder[]{
                new DefaultFieldBinder(sc_report.name,150)
            },
                    new DataPanelInterface[]{
                new DataPanelScheduleReport(connection,name,option),
                //new DataPanelScheduleReportRecipient(connection)
                new EntityEditorCombiner(
                        connection,
                        new PanelScheduleReportSource(connection,null,name,option),
                        "Reports"
                        ),
                new EntityEditorCombiner(
                        connection,
                        new PanelScheduleReportRecipient(connection,null),
                        "Recipients"
                        )
            }
            );
            setListPreferredSize(new Dimension(150, 300));
            if (bind_field != null) {
                setOtherWhere(
                        "bindable = '"+
                        bind_field.getBindable().getClass().getName() +
                        "' and bindable_id = '"+
                        bind_field.getValueAsString()+"'"
                        );
                addNewEventListner(this);
            } else {
                setOtherWhere("bindable is null");
            }
        } catch (BindException be) {
            throw new DataException(be);
        }
    }
    
    public PanelScheduleReport(DBConnection connection,BindFieldInterface bind_field)
    throws DataException, SQLException {
        this(connection,bind_field,null,null);
    }
    
    public PanelScheduleReport(DBConnection connection,String name[],String option[])
    throws DataException, SQLException {
        this(connection,null,name,option);
    }
    
    public boolean eventNew(Entity entity)
    throws DataException, SQLException{
        try {
            if (bind_field != null) {
                ((ScheduleReport) entity).bindable.set(bind_field.getBindable().getClass().getName());
                ((ScheduleReport) entity).bindable_id.set(bind_field.getValueAsString());
            }
            return true;
        } catch (Exception exception) {
            throw new DataException(exception);
        }
    }
    
    public PanelScheduleReport(DBConnection connection)
    throws DataException, SQLException {
        this(connection,null);
    }
    
    public void selected() {}
    public void setTabbedpaneGroup(TabbedpaneGroup parent) {}
    public void activate() {}
    public void deactivate() {}
    
    public static class DataPanelScheduleReport extends DataPanel {
        
        private DBConnection connection = null;
        private String name[]=null,option[]=null;
        
        public DataPanelScheduleReport(DBConnection connection) {
            super();
            this.connection = connection;
        }
        
        public DataPanelScheduleReport(DBConnection connection,String name[],String option[]) {
            this(connection);
            this.name = name;
            this.option = option;
        }
        
        public String getName() {
            return "Schedule";
        }
        
        public void build(Binder binder, Bindable entity)
        throws IOException, BindException {
            try{
                ScheduleReport sc_report = (ScheduleReport)entity;
                /*
                if (option != null) {
                    sc_report.report.setOption(option,name);
                }*/
                DataGroup type = new DataGroup("Schedule a report");
                //type.add(binder.createDataPair(sc_report.report));
                //type.add(binder.createDataPair(sc_report.file_name));
                type.add(binder.createDataPair(sc_report.name));
                type.add(binder.createDataPair(sc_report.subject));
                type.add(binder.createDataPair(sc_report.e_message));
                //type.add(binder.createDataPair(sc_report.export_type));
                type.add(binder.createDataPair(sc_report.frequency));
                type.add(binder.createDataPair(sc_report.parameter_frequency));
                type.add(binder.createDataPair(sc_report.parameter_frequency_offset));
                type.add(binder.createDataPair(sc_report.r_time));
                type.add(binder.createDataPair(sc_report.r_date));
                addGroup(type);
            } catch (Exception exception) {
                throw new BindException(exception);
            }
        }
        public void activated() {}
        public void deactivated() {}
    }
    
    public static class PanelScheduleReportSource extends EntityEditor implements EntityEditorCombiner.CombineEntity {
        
        private ScheduleReport scheduled_report = null;
        private String [] name = null,option  = null;
        
        public PanelScheduleReportSource(DBConnection connection,ScheduleReport scheduled_report,String name[],String option[])
        throws DataException, SQLException {
            super(connection,"Reports");
            this.name= name;
            this.option = option;
            if (scheduled_report == null) {
                return;
            }
            this.scheduled_report = scheduled_report;
            try {
                ScheduleReportSource source=new ScheduleReportSource(connection);
                setDetails(
                        source,
                        new EntityPanelFieldBinder[]{
                    new DefaultFieldBinder(source.file_name,150)
                },
                        new DataPanelInterface[]{
                    new DataPanelScheduleReportSource(connection,scheduled_report,name,option)
                }
                );
                setOtherWhere("schedule_report_id = " + Entity.asSQL(scheduled_report.id.get()));
            } catch (BindException be) {
                throw new DataException(be);
            }
        }
        
        public EntityEditor newInstance(DBConnection connect,Binder binder,Bindable bindable)
        throws SQLException {
            return new PanelScheduleReportSource(connect,(ScheduleReport) bindable,name,option);
        }
        
        public static class DataPanelScheduleReportSource extends DataPanel {
            private DBConnection connection = null;
            private ScheduleReport scheduled_report = null;
            private String [] name = null,option = null;
            
            public DataPanelScheduleReportSource(DBConnection connection,ScheduleReport scheduled_report,String name[],String option[]) {
                super();
                this.name = name;
                this.option = option;
                this.connection = connection;
                this.scheduled_report = scheduled_report;
            }
            
            public String getName() {
                return "Reports";
            }
            
            public void build(Binder binder, Bindable entity)
            throws IOException, BindException {
                try{
                    ScheduleReportSource srr = (ScheduleReportSource)entity;
                    if (option != null) {
                        srr.report.setOption(option,name);
                    }
                    srr.schedule_report_id.set(scheduled_report.id.get());
                    DataGroup type = new DataGroup("Reports");
                    type.add(binder.createDataPair(srr.report));
                    type.add(binder.createDataPair(srr.file_name));
                    type.add(binder.createDataPair(srr.export_type));
                    addGroup(type);
                } catch (Exception exception) {
                    exception.printStackTrace();
                    throw new BindException(exception);
                }
            }
            
            public void activated() {}
            public void deactivated() {}
        }
    }
    
    
    public static class PanelScheduleReportRecipient extends EntityEditor implements EntityEditorCombiner.CombineEntity {
        
        private ScheduleReport scheduled_report = null;
        
        public PanelScheduleReportRecipient(DBConnection connection,ScheduleReport scheduled_report)
        throws DataException, SQLException {
            super(connection,"Report Recipients");
            if (scheduled_report == null) {
                return;
            }
            this.scheduled_report = scheduled_report;
            try {
                ScheduleReportRecipient srr=new ScheduleReportRecipient(connection);
                setDetails(
                        srr,
                        new EntityPanelFieldBinder[]{
                    new DefaultFieldBinder(srr.recipient,150)
                },
                        new DataPanelInterface[]{
                    new DataPanelScheduleReportRecipient(connection,scheduled_report)
                }
                );
                setOtherWhere("schedule_report_id = " + Entity.asSQL(scheduled_report.id.get()));
            } catch (BindException be) {
                throw new DataException(be);
            }
        }
        
        public EntityEditor newInstance(DBConnection connect,Binder binder,Bindable bindable)
        throws SQLException {
            return new PanelScheduleReportRecipient(connect,(ScheduleReport) bindable);
        }
        
        public static class DataPanelScheduleReportRecipient extends DataPanel {
            private DBConnection connection = null;
            private ScheduleReport scheduled_report = null;
            
            public DataPanelScheduleReportRecipient(DBConnection connection,ScheduleReport scheduled_report) {
                super();
                this.connection = connection;
                this.scheduled_report = scheduled_report;
            }
            
            public String getName() {
                return "Report Recipient";
            }
            
            public void build(Binder binder, Bindable entity)
            throws IOException, BindException {
                try{
                    ScheduleReportRecipient srr = (ScheduleReportRecipient)entity;
                    srr.schedule_report_id.set(scheduled_report.id.get());
                    DataGroup type = new DataGroup("Send report to recipient");
                    type.add(binder.createDataPair(srr.recipient));
                    addGroup(type);
                } catch (Exception exception) {
                    exception.printStackTrace();
                    throw new BindException(exception);
                }
            }
            
            public void activated() {}
            public void deactivated() {}
        }
    }
}
